package cn.gogoyc.org.controller;

import cn.gogoyc.org.basic.PreAuthorize;
import cn.gogoyc.org.basic.exception.BusinessException;
import cn.gogoyc.org.basic.util.AjaxResult;
import cn.gogoyc.org.basic.util.ExcelUtils;
import cn.gogoyc.org.basic.util.LoginContext;
import cn.gogoyc.org.basic.util.PageList;
import cn.gogoyc.org.domain.Shop;
import cn.gogoyc.org.domain.ShopAuditLog;
import cn.gogoyc.org.basic.dto.ShopDto;
import cn.gogoyc.org.query.ShopQuery;
import cn.gogoyc.org.service.ShopService;
import cn.gogoyc.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 店铺后端接口类；
 * @author L1vezZ
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

    /**
     * The Shop service.
     */
    @Autowired
    public ShopService shopService;


    /**
     * 接口：添加或修改
     *
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "店铺添加或删除",sn = "shop:getAll")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.save(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败", false);
        }
    }

    /**
     * 接口：删除
     *
     * @param id the id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败", false);
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids the ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败", false);
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id the id
     * @return the shop
     */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.loadById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return list list
     */
    @GetMapping
    public List<Shop> findAll() {
        return shopService.loadAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 接口:注册入驻信息过滤
     *
     * @param shop the shop
     * @return the
     */
    @PostMapping(value = "/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙...", false);
        }
    }

    /**
     * 接口:驳回用户
     *
     * @param log the log
     * @return the ajax result
     */
    @PostMapping(value = "/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog log, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            shopService.auditReject(log,logininfo);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙...", false);
        }
    }

    /**
     * 接口:审查对象并修改状态
     *
     * @param log the log
     * @return the ajax result
     */
    @PostMapping(value = "/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog log, HttpServletRequest request) {
        try {
            Logininfo logininfo = LoginContext.getLogininfo(request);
            shopService.auditPass(log,logininfo);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(e.getMessage(), false);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙...", false);
        }
    }

    /**
     * 接口:导出报表
     *
     * @param response the response
     */
    @GetMapping("/export")
    public void excelExport(HttpServletResponse response) {
        List<Shop> shops = shopService.loadAll();
        ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xls", response);
    }

    /**
     * 接口:导入报表
     *
     * @param file the file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        //             文件对象             忽略标题行数        护理表头行数             生成的对象类型
        List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
    }

    /**
     * 接口:邮箱激活
     *
     * @param id the id
     * @return the string
     */
    @GetMapping("/active/{id}")
    public String activatedState(@PathVariable("id") Long id) {
        try {
            shopService.activatedState(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "<h1>激活成功<h1>";
    }

    @GetMapping("/echarts")
    public List<ShopDto> echarts(){
        return shopService.getNumAndState();
    }

}
